package icu.cocoon.game.api;

import icu.cocoon.dao.base.Resp;
import icu.cocoon.game.entity.Proverb;
import icu.cocoon.game.service.ProverbService;
import icu.cocoon.util.ValidatorUtil;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/game/proverb")
public class ProverbApiController {

  @Resource
  private ProverbService proverbService;

  @GetMapping("/keyword/{keyword}")
  public Resp<List<Proverb>> searchByKeyword(@PathVariable String keyword) {
    if (keyword.length() < 1) {
      return Resp.success();
    }
    if (ValidatorUtil.isChineseFull(keyword)){
      keyword = keyword.substring(0, 1);
      List<Proverb> proverbList = proverbService.searchByWord(keyword);
      return Resp.success(proverbList);
    }
    List<Proverb> proverbList = proverbService.searchByPinyin(keyword);
    return Resp.success(proverbList);
  }

}
